﻿using System;
using MMK.Paket_Tracker.Runtime;
using MMK.Paket_Tracker.GUI.WebServiceReference;
using System.Windows.Media;
using System.IO;
using System.Windows.Media.Imaging;

namespace MMK.Paket_Tracker.GUI.Models
{
    /// <summary>
    /// Model for GetStatus
    /// </summary>
    public class GetStatusModel : ModelBase
    {
        internal void GetStatus()
        {
            ImageSourceConverter conv = new ImageSourceConverter();
            Byte[] image;
            paketStatus = client.GetPaketStatus(PacketID);
            if (paketStatus != null)
            {
                image = paketStatus.MapImage;
                Status = paketStatus.Beschreibung;
                if (image != null)
                {
                    MemoryStream ms = new MemoryStream(image);
                    StatusImage = BitmapFrame.Create(ms);
                }
            }
            else
            {
                Status = "Paket-ID unbekannt...";
            }
        }

        /// <summary>
        /// Gets or sets the packet ID.
        /// </summary>
        /// <value>The packet ID.</value>
        public String PacketID
        {
            get { return packetID; }
            set 
            { 
                packetID = value;
                CheckPacketID();
                RaisePropertyChanged("PacketID"); 
            }
        }

        /// <summary>
        /// Gets or sets the status image.
        /// </summary>
        /// <value>The status image.</value>
        public ImageSource StatusImage
        {
            get { return statusImage; }
            set 
            { 
                statusImage = value; 
                RaisePropertyChanged("StatusImage"); 
            }
        }


        /// <summary>
        /// Gets or sets the error text.
        /// </summary>
        /// <value>The error text.</value>
        public String ErrorText
        {
            get { return errorText; }
            set 
            { 
                errorText = value;
                RaisePropertyChanged("ErrorText");
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has error.
        /// </summary>
        /// <value><c>true</c> if this instance has error; otherwise, <c>false</c>.</value>
        public Boolean HasError
        {
            get
            {
                return hasError;
            }
        }

        private void CheckPacketID()
        {
            String error = String.Empty;

            if (String.IsNullOrEmpty(PacketID) == false)
            {
                hasError = false;

                if (PacketID.Length < 15)
                {
                    hasError = true;
                    error += "Die ID ist zu kurz.";
                }
                else
                {
                    Char[] charArray = PacketID.ToCharArray();
                    Boolean isFirst = true;

                    foreach (var item in charArray)
                    {
                        if (((item.CompareTo('A') >= 0 && item.CompareTo('Z') <= 0) || (item.CompareTo('0') >= 0 && item.CompareTo('9') <= 0)) == false)
                        {
                            hasError = true;
                            if (isFirst == true)
                            {
                                error += "Die Paket-ID enthält ungültige Zeichen.";
                                isFirst = false;
                            }
                        }
                    }
                }
            }
            else
            {
                hasError = true;
            }

            if (hasError == false)
            {
                error = "Eingabe ok, klicken Sie auf 'Weiter'.";
            }
            
            ErrorText = error;

        }
        
        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        /// <value>The status.</value>
        public String Status
        {
            get { return status; }
            set
            { 
                status = value;
                RaisePropertyChanged("Status");
            }
        }
        
        private WebservicesSoapClient client = new WebservicesSoapClient();
        private PaketStatus paketStatus;
        private String packetID;
        private ImageSource statusImage;
        private String status = "[noch nicht ermittelt]";
        private Boolean hasError = true;
        private String errorText;

    }
}
